package cn.edu.nuc.film.controller;


import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import cn.edu.nuc.film.common.PageControl;
import cn.edu.nuc.film.common.Pager;
import cn.edu.nuc.film.entity.Film;
import cn.edu.nuc.film.entity.Review;
import cn.edu.nuc.film.entity.User;
import cn.edu.nuc.film.service.interfaces.FilmService;
import cn.edu.nuc.film.service.interfaces.ReviewService;

@Controller
@RequestMapping(value="/film/review")
public class ReviewController {
	
	@Autowired
	private ReviewService reviewService;
	
	@Autowired
	private FilmService filmService;

	@RequestMapping(value = "/reviewList", method = RequestMethod.GET)
	public String reviewList(String sname, HttpServletRequest request) {
		// 获取要查询的影片名字
		// String sname = request.getParameter("sname");
		if (sname == null || sname.equals("")) {
			sname = null;
		}
		/* System.out.println("sname--" + sname); */
		// 设置分页信息
		PageControl pc = new PageControl();

		// 设置初始当前页currindex = 1
		Integer currindex = 1;

		// 如果获取的从前台获取的index(当前页)不为空，则赋值给 currindex(分页当前页)
		if (request.getParameter("index") != null) {
			currindex = Integer.parseInt(request.getParameter("index"));
		}

		// 将获取的当前页设置在分页信息(PageControl)里
		pc.setCurrentindex(currindex);
		pc.setPagesize(5);

		// 调用service中的分页方法，获取分页信息
		Pager<Film> pager = filmService.filmList(pc, sname);

		// 将分页信息传送到前台
		request.setAttribute("pager", pager);

		// 返回filmlist页面
		return "sys/review/reviewList";
	}

	@RequestMapping(value = "/toWriteReview", method = RequestMethod.GET)
	public String toWriteReview(int filmId, Model model, HttpServletRequest request) {
		// 获取用户信息
		HttpSession session = request.getSession();
		User user = (User) session.getAttribute("user");
		if( user == null){
			return "redirect:reviewList";
		}else{
			// 获取影片信息
			Film film = filmService.detail(filmId);
			model.addAttribute("user", user);
			model.addAttribute("film", film);
			return "sys/review/writeReview";
		}
		//System.out.println(user.getUserid());
		
		
	}
	
	/**
	 * 发表影评
	 * @param review
	 * @param model
	 * @return
	 */
	@RequestMapping(value="/saveReview", method=RequestMethod.POST)
	public String writeReview( Review review,Model model) {
		
		
		/*//获取film的详细信息
		Film film = filmService.pageFilmDetail(filmId);
		//获取film的评论
		List<Review> reviewList = reviewService.reviewList(filmId);
		model.addAttribute("film", film);
		model.addAttribute("reviewList", reviewList);
		return "/page/film_info";*/
		
		
		int count = reviewService.saveReview(review);
		if( count > 0 ){
			return "redirect:reviewList";
		}else{
			model.addAttribute("msg", "发表影评失败!");
			return "sys/review/writeReview";
		}
		
	}
	
	
	
	/**
	 * 发表影评
	 * @param review
	 * @param model
	 * @return
	 */
	@RequestMapping(value="/indexsaveReview", method=RequestMethod.POST)
	public String writeReview1( Review review,Model model) {
		
		int count = reviewService.saveReview(review);
		//获取film的详细信息
		Film film = filmService.pageFilmDetail(review.getFilmId());
		//获取film的评论
		List<Review> reviewList = reviewService.reviewList(review.getFilmId());
		model.addAttribute("film", film);
		model.addAttribute("reviewList", reviewList);
		return "/page/film_info";
		
		
	}
	
	
	
	@RequestMapping(value = "/lookReviewList", method = RequestMethod.GET)
	private String lookReviewList(int filmId,  HttpServletRequest request) {

		// 设置分页信息
		PageControl pc = new PageControl();

		// 设置初始当前页currindex = 1
		Integer currindex = 1;

		// 如果获取的从前台获取的index(当前页)不为空，则赋值给 currindex(分页当前页)
		if (request.getParameter("index") != null) {
			currindex = Integer.parseInt(request.getParameter("index"));
		}

		// 将获取的当前页设置在分页信息(PageControl)里
		pc.setCurrentindex(currindex);
		pc.setPagesize(5);

		// 调用service中的分页方法，获取分页信息
		Pager<Review> pager = reviewService.list( pc,filmId);
		request.setAttribute("pager", pager);
		return "sys/review/lookReviewList";

	}
	
	
	
	
	
	
	
	

}
